package com.sakura.probe.common.util;

import com.sakura.probe.common.api.FileStateEnum;
import com.sakura.probe.dto.ImageHolder;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class HttpServletRequestUtil {
    //文件最大上传量为10M
    private static final long MAX_UPLOAD_SIZE = 1024 * 1024 * 10;
    public static int getInt(HttpServletRequest request, String name){
        try {
            return Integer.decode(request.getParameter(name));
        } catch (Exception e) {
            return -1;
        }
    }
    public static long getLong(HttpServletRequest request, String name) {

        try {
            return Long.valueOf(request.getParameter(name));
        } catch (Exception e) {
            return -1;
        }
    }

    public static List<Long> getLongList(HttpServletRequest request, String name,int size){
        List<Long> ans = new ArrayList<Long>();
        for (int i=0;i<size;i++){
            Long id = HttpServletRequestUtil.getLong(request,name+i);
            if (id!=-1L){
                ans.add(id);
            }else {
                break;
            }
        }
        return ans;
    }

    public static Double getDouble(HttpServletRequest request, String name) {

        try {
            return Double.valueOf(request.getParameter(name));
        } catch (Exception e) {
            return -1d;
        }
    }

    public static Boolean getBoolean(HttpServletRequest request, String name) {

        try {
            return Boolean.valueOf(request.getParameter(name));
        } catch (Exception e) {
            return false;
        }
    }

    public static String getString(HttpServletRequest request, String name) {
        try {
            String result = request.getParameter(name);
            if (result != null) {
                result = result.trim();
            }
            if ("".equals(result))
                result = null;
            return result;
        } catch (Exception e) {
            return null;
        }

    }

    public static ImageHolder getMultipartFile(HttpServletRequest request, String name){
        ImageHolder holder ;
        //获取文件的图片输入流
        MultipartHttpServletRequest multipartHttpServletRequest = null;
        MultipartFile multipartFile = null;
        CommonsMultipartResolver resolver = new CommonsMultipartResolver(request.getSession().getServletContext());
        try {
            //判断是否上传了文件
            if (resolver.isMultipart(request)){
                multipartHttpServletRequest = (MultipartHttpServletRequest) request;
                multipartFile = multipartHttpServletRequest.getFile(name);
                if (multipartFile.getSize()>MAX_UPLOAD_SIZE){
                    holder = new ImageHolder(FileStateEnum.MAX_SIZE_FILE);
                }else {
                    holder = new ImageHolder(FileStateEnum.GET_FILE_SUCCESS,multipartFile);
                }
            }else {
                holder = new ImageHolder(FileStateEnum.NULL_FILE);
            }
        } catch (Exception e) {
            holder = new ImageHolder(FileStateEnum.GET_FILE_FAIL);
        }
        return holder;
    }

}
